package com.itheima.controller;

import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "套餐管理开发")  // 作用在类上
public class SetmealController {

    @Reference
    private SetmealService setmealService;

 /*   @Autowired
    private RedisTemplate redisTemplate;*/


      //todo:分页查询
    @PostMapping("setmeal/findPage")//post请求
    @ApiOperation(value = "分页查询", notes = "根据客户端请求，完成分页查询")//   添加业务方法上
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前分页页码", required = true, paramType = "form", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true, paramType = "form", dataType = "Integer"),
            @ApiImplicitParam(name = "queryString", value = "查询条件", required = false, paramType = "form", dataType = "String")
    })
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
     //  redisTemplate.opsForValue().set();根据不同的数据类型进行操作

       // System.out.println(queryPageBean.getQueryString());
        return new Result(setmealService.findPage(queryPageBean)); //   total  +  List

    }


    //todo: 添加 后期加上修改
    @PostMapping("setmeal/add")
    public Result add(@RequestBody SetmealDTO setmealDTO) { //接收前端发来的数据
        /* checkitem  有 id  托管态 执行 update xx set xxx? x? where id -?
         * checkitem  传来没有id 瞬时态 没有id 执行 save 方法 insert
         * */
        //更改后 添加或者修改的代码
        return new Result(setmealService.saveSetmeal(setmealDTO));
    }


       // @DeleteMapping("checkgroup/deleteCheckItemById/{id}")
    //public  Result deleteCheckGroupById(@PathVariable("id")int id){
    //
    //     return new Result(checkGroupService.updateCheckGroupById(id));
    // }

     //todo:文件上传
     @PostMapping("setmeal/upload") //图片上传 不需要操作数据库 上传到阿里云
    public  Result upload (@RequestParam("imgFile")MultipartFile file){//MF就是文件上传的对象
         try {
             //MultipartFile file就是文件上传的对象
             String originalFilename = file.getOriginalFilename();//获得原始的文件名
             String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);//把原始的文件名转化为uuid 的唯一名字
             //调用阿里云的 sdk   上传图pain方法
             AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidfilename);
             //图片信息保存 set 集合 redis保存一份 并且设置:延迟时间1分钟
             RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);//放一份图片信息到set集合
             //把图片存储到Redis 保存1分钟 超过1分钟 redis 自动删除该图片信息
             RedisUtil.set(RedisConstant.SINGLE_PIC+uuidfilename,uuidfilename,1, TimeUnit.MINUTES);//todo:设置保存1分钟

             return new Result(uuidfilename);//这是阿里云上的文件名
         } catch (IOException e) {
             e.printStackTrace();
             throw  new RuntimeException(e.getMessage());
   ///
         }

     }



}








